<template>
  <div class="newProject">
    <div class="itemList">
      <div class="item">
        <div class="item_name">项目名称</div>
        <input type="text" placeholder="输入项目名称" v-model="project_name">     
      </div>
      <div class="desc">
        <div class="item_name" >项目描述</div>
        <textarea v-model="project_desc"></textarea>
      </div>
      <div class="item">
        <div class="item_name">收货方式</div>
        <picker
          @change="receiptType"
          :value="receiptType_index"
          :range="receiptType_data"
        >
          <!-- <input type="text"  v-model="selectType_input"> -->
          <div class="receiptType_content">
            <div>{{receiptType_input}}</div>
            <span>></span>
          </div>

        </picker> 
      </div>


      <div class="btn">
        <div class="btn_commit" @click="commit" v-if="project_id">保存项目</div>
        <div class="btn_commit" @click="commit" v-else>新建项目</div>
      </div>
    </div>
  </div>
</template>
<script>
import { setTimeout } from 'timers';
export default {
  data() {
    return {
      project_id:'',
      project_item:'',
      project_name:'',
      project_desc:'',
      receiptType_data: ['理计', '磅计'],
      receiptType_index:[0],
      receiptType_input:'',
      statusType_data: ['未启用', '已启用','完工'],
      statusType_index:[0],
      statusType_input:'',

    }
  },
  methods: {
    receiptType(e){
      this.receiptType_index=e.target.value
      this.receiptType_input=this.receiptType_data[this.receiptType_index]

    },
    statusType(e){
      this.statusType_index=e.target.value
      this.statusType_input=this.statusType_data[this.statusType_index]
    },
    async commit(){
      if(this.receiptType_input=='理计'){
        var receipt_way='rational'
      }else if(this.receiptType_input=='磅计'){
        var receipt_way='pound_meter'
      }
      if(this.statusType_input=='未启用'){
        var status=0
      }else if(this.statusType_input=='已启用'){
        var status=1
      }else if(this.statusType_input=='完工'){
        var status=2
      }
      
      const userToken=wx.getStorageSync('userToken')
      const res=await this.$request.postData('/ucenter/buildProject',{
        user_token:userToken,
        name:this.project_name,
        desc:this.project_desc,
        receipt_way,
        status,
        id:this.project_id
      })
      if(res.data.level=='success'){
        this.$currency.showToast(res.data.message)
        setTimeout(async()=>{
          const select=await this.$currency.showModal('是否创建点条','')
          if(select){
            this.$currency.navigateTo(`/pages/countSteel/main?id=${res.data.data.id}&name=${res.data.data.name}`)
          }else{
            wx.navigateBack({
              delta:1
            })
          }
        },1500)
      }else{
        this.$currency.showToast(res.data.message)
      }
    },
    initData(){
      this.project_id='';
      this.project_item='';
      this.project_name='';
      this.project_desc='';
      this.receiptType_data= ['理计', '磅计'];
      this.receiptType_index=[0];
      this.receiptType_input='';
      this.statusType_data= ['未启用', '已启用','完工'];
      this.statusType_index=[0];
      this.statusType_input='';
    }

  },
  onLoad(options){
    this.initData()
    if(options.item){
      this.project_item=JSON.parse(options.item);
      this.project_name=this.project_item.name;
      this.project_desc=this.project_item.desc;
      this.receiptType_input=this.project_item.receipt_way;
      this.statusType_input=this.project_item.status;
      this.project_id=this.project_item.id
    }
  }

}
</script>
<style lang="scss" scoped>
  .newProject{

    width: calc(100% - 40px);
    margin-left: 20px;
    font-size: 18px;
  }
  .item{
    display: flex;
    align-items: center;
    padding:20px 0;
    height: 25px;
    line-height: 25px;
    border-bottom: 1px solid #eee;
    .item_name{
      width: 100px;
    }
    & input{
      margin-left:20px;
    }
  }
  .desc{
    margin-top:10px;
  }
  textarea{
    margin-top:10px;
  }
  .receiptType_content,.statusType_content{
    margin-left: 20px;
    width:230px;
    height:25px;
    display:flex;
    justify-content: space-between;
  }
  .upLoadImg{
    img{
      margin-left: 20px;
      width: 80px;
      height: 80px;
    }
  }

  .btn{
    margin-top:20px;
    margin-bottom: 20px;
    display: flex; 
    justify-content: space-around;
  }
  .btn_commit,.btn_save{
    width: 120px;
    height: 60px;
    line-height: 60px;
    color:#fff;
    text-align: center;
    border-radius: 10px;
    background: rgb(126, 143, 221);
  }
  .btn_save{
    background: rgb(140, 224, 140);
  }
</style>

